home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Technology Demos and Tools.iso / java / demo / ImageMap / AniArea.java < prev    next >
Encoding:
Java Source  |  1996-04-26  |  3.1 KB  |  102 lines

  1. /*
  2.  * @(#)AniArea.java    1.7 96/04/24  
  3.  *
  4.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.util.StringTokenizer;
  34. import java.awt.Image;
  35. import java.net.URL;
  36. import java.net.MalformedURLException;
  37.  
  38. /**
  39.  * This ImageArea provides for a button that animates when the mouse is
  40.  * over it. The animation is specifed as a base image that contains all
  41.  * of the animation frames and then a series of X,Y coordinate pairs that
  42.  * define the top left corner of each new frame.
  43.  *
  44.  * @author    Chuck McManis
  45.  * @version    1.7, 04/24/96
  46.  */
  47. class AniArea extends ImageMapArea {
  48.  
  49.     Image sourceImage;
  50.     int     nFrames;
  51.     int  coords[];
  52.     int     currentFrame = 0;
  53.  
  54.     public void handleArg(String s) {
  55.     StringTokenizer st = new StringTokenizer(s, ", ");
  56.     int    i;
  57.         String imgName;
  58.  
  59.     imgName = st.nextToken();
  60.     try {
  61.         sourceImage = parent.getImage(new URL(parent.getDocumentBase(),
  62.                           imgName));
  63.         parent.addImage(sourceImage);
  64.     } catch (MalformedURLException e) {}
  65.  
  66.     nFrames = 0;
  67.     coords = new int[40];
  68.  
  69.     while (st.hasMoreTokens()) {
  70.         coords[nFrames*2]     = Integer.parseInt(st.nextToken());
  71.         coords[(nFrames*2)+1] = Integer.parseInt(st.nextToken());
  72.         nFrames++;
  73.         if (nFrames > 19)
  74.         break;
  75.     }
  76.     }
  77.  
  78.     public boolean animate() {
  79.     if (entered) {
  80.         repaint();
  81.     }
  82.     return entered;
  83.     }
  84.  
  85.     public void enter() {
  86.     currentFrame = 0;
  87.     parent.startAnimation();
  88.     }
  89.  
  90.     public void highlight(Graphics g) {
  91.     if (entered) {
  92.         drawImage(g, sourceImage, 
  93.               X-coords[currentFrame*2], Y-coords[(currentFrame*2)+1],
  94.               X, Y, W, H);
  95.         currentFrame++;
  96.         if (currentFrame >= nFrames)
  97.         currentFrame = 0;
  98.     }
  99.     }
  100. }
  101.  
  102.